package net.dopan.pigframe.oss.service.impl;

import com.google.gson.Gson;
import net.dopan.pigframe.core.base.service.impl.BaseServiceImpl;
import net.dopan.pigframe.core.exception.RRException;
import net.dopan.pigframe.oss.mapper.OSSConfigMapper;
import net.dopan.pigframe.oss.mapper.OSSMapper;
import net.dopan.pigframe.oss.model.entity.OSSConfig;
import net.dopan.pigframe.oss.service.OSSConfigService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Package: net.dopan.pigframe.oss.service.impl
 * @Title: OSSConfigServiceImpl
 * @Description: OSS配置
 * @author: 刘宽
 * @date: 2018/8/15 17:03
 */
@Service("ossConfigService")
public class OSSConfigServiceImpl extends BaseServiceImpl<OSSConfigMapper, OSSConfig> implements OSSConfigService {

    @Autowired
    private OSSConfigMapper ossConfigMapper;

    @Override
    public void updateValueByKey(String key, String value, Integer id) {
        ossConfigMapper.updateValueByKey(key, value, id);
    }

    @Override
    public String getValue(String key) {
        return ossConfigMapper.queryByKey(key);
    }

    @Override
    public <T> T getConfigObject(String key, Class<T> clazz) {
        String value = getValue(key);
        if(StringUtils.isNotBlank(value)){
            return new Gson().fromJson(value, clazz);
        }

        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new RRException("获取参数失败");
        }
    }
}
